package com.icehand.utils.date;

import org.joda.time.DateTime;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 * Created by icehand on 2017/5/3.
 */
public class DateUtils {

	/**
	 * 取得当前日期所在周的第一天
	 * @param date date
	 * @return date
	 */
	public static Date getFirstDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.SUNDAY);
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); // Sunday
		return calendar.getTime();
	}

	public static int getFirstDayOfWeek(Integer timestamp) {
		Date date = new Date((long) timestamp * 1000);
		Date firstDayOfWeek = getFirstDayOfWeek(date);

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(firstDayOfWeek);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		;
		return (int)(calendar.getTimeInMillis()/1000);
	}

	public static int getLastDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.SUNDAY);
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK,
				calendar.getFirstDayOfWeek() + 6);// Saturday
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND,59);
		calendar.set(Calendar.MILLISECOND, 59);
		return (int) (calendar.getTimeInMillis()/1000);
	}

	public static int getLastDayOfWeek(Integer timestamp){
		Date date = new Date((long) timestamp * 1000);
		return getLastDayOfWeek(date);
	}

	public static Date getNow() {
		return DateTime.now().toDate();
	}

	public static String getCurrentString() {
		return DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
	}

	public static Long getCurrentLong() {
		return DateTime.now().getMillis();
	}

	public static String getOrderTimeString() {
		return DateTime.now().toString("yyMMddHHmmss");
	}

	/**
	 * 时间比较, 如果d1>d2,则返回true
	 * @param d1 date
	 * @param d2 date
	 * @return boolean
	 */
	public static boolean compareTime(Date d1, Date d2) {
		return d1.getTime() - d2.getTime()>=0;
	}

	public static boolean compareToCurrentTime(Date d2) {
		return compareTime(new Date(), d2);
	}

	public static Integer getNowTimestamp() {
		return (int) (System.currentTimeMillis() / 1000);
	}

	/**
	 * 一天的起始时间
	 * @param timestamp 时间戳
	 * @return int
	 */
	public static int getStartTime(Long timestamp){
		Calendar todayStart = Calendar.getInstance();
		todayStart.setTimeInMillis(timestamp);
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return (int) (todayStart.getTimeInMillis() / 1000);
	}

	/**
	 * 一天的结束时间
	 * @param timestamp 时间戳
	 * @return int
	 */
	public static int getEndTime(Long timestamp){
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.setTimeInMillis(timestamp);
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return (int) (todayEnd.getTimeInMillis() / 1000);
	}

	public static int getTomorrowEndTime(Long timestamp){
		Calendar tomorrow = Calendar.getInstance();
		tomorrow.setTimeInMillis(timestamp);
		tomorrow.add(Calendar.DATE,1);
		tomorrow.set(Calendar.HOUR_OF_DAY, 23);
		tomorrow.set(Calendar.MINUTE, 59);
		tomorrow.set(Calendar.SECOND, 59);
		tomorrow.set(Calendar.MILLISECOND, 999);
		return (int) (tomorrow.getTimeInMillis() / 1000);
	}

	public static int getTomorrowStartTime(long timestamp) {
		Calendar tomorrowStart = Calendar.getInstance();
		tomorrowStart.setTimeInMillis(timestamp);
		tomorrowStart.add(Calendar.DATE,1);
		tomorrowStart.set(Calendar.HOUR_OF_DAY, 0);
		tomorrowStart.set(Calendar.MINUTE, 0);
		tomorrowStart.set(Calendar.SECOND, 0);
		tomorrowStart.set(Calendar.MILLISECOND,0);
		return (int) (tomorrowStart.getTimeInMillis() / 1000);
	}

	/**
	 * 前七天的时间
	 * @param timestamp long
	 * @return int
	 */
	public static int getLastWeekTime(long timestamp){
		Calendar lastWeek = Calendar.getInstance();
		lastWeek.setTimeInMillis(timestamp);
		lastWeek.add(Calendar.DATE, - 7);
		lastWeek.set(Calendar.HOUR_OF_DAY, 0);
		lastWeek.set(Calendar.MINUTE, 0);
		lastWeek.set(Calendar.SECOND, 0);
		lastWeek.set(Calendar.MILLISECOND,0);
		return (int) (lastWeek.getTimeInMillis() / 1000);
	}

	/**
	 *
	 * @param date date
	 */
	public static String formatTime(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(date);
	}

	/**
	 * 获取日期
	 * @param time int
	 * @return Object
	 */
	public static Object getDay(Integer time) {
		if(null == time){
			time = getNowTimestamp();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(Long.valueOf(time)*1000);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 以今天为中心轴
	 * @param day 0代表今天
	 * @param hour 小时
	 * @param minute 分钟
	 * @param second 秒
	 * @param millis 毫秒
	 * @return int
	 */
	public static int getTimestamp(int day, int hour, int minute, int second, int millis) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, day);
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, minute);
		cal.set(Calendar.SECOND, second);
		cal.set(Calendar.MILLISECOND,millis);
		return (int) (cal.getTimeInMillis() / 1000);
	}
}
